package com.demo.touca.service.job;

import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.core.ExitStatus;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.UUID;

/**
 * 多步骤任务
 */
@Slf4j
@Component
public class MultiJobConfig {
    @Autowired
    private JobBuilderFactory jobBuilderFactory;
    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    @Bean("multiStepJob")
    public Job multiStepJob() {
        return jobBuilderFactory.get("multiStepJob")
                .start(step1())
                .on(ExitStatus.COMPLETED.getExitCode()).to(step2())
                .from(step2())
                .on(ExitStatus.COMPLETED.getExitCode()).to(step3())
                .from(step3()).end()
                .build();
    }

    private Step step1() {
        return stepBuilderFactory.get("step1")
                .tasklet((stepContribution, chunkContext) -> {
                    // 获取参数
                    Map<String, Object> paramMap = chunkContext.getStepContext().getJobParameters();
                    log.info("param={}", paramMap);
                    System.out.println("执行步骤一操作。。。");
                    // 在step内设置的参数
                    ExecutionContext executionContext = stepContribution.getStepExecution().getJobExecution().getExecutionContext();
                    executionContext.put("from", "step1");
                    String uuid = UUID.randomUUID().toString();
                    executionContext.put("nonce", uuid);
                    log.info("uuid={}", uuid);
                    return RepeatStatus.FINISHED;
                }).build();
    }

    private Step step2() {
        return stepBuilderFactory.get("step2")
                .tasklet((stepContribution, chunkContext) -> {
                    // 获取参数
                    Map<String, Object> paramMap = chunkContext.getStepContext().getJobParameters();
                    log.info("param={}", paramMap);
                    System.out.println("执行步骤二操作。。。");
                    // 获取step内设置的参数
                    Map<String, Object> stepParamMap = chunkContext.getStepContext().getJobExecutionContext();
                    String from = (String) stepParamMap.get("from");
                    String uuid = (String) stepParamMap.get("nonce");
                    log.info("from={},uuid={}", from, uuid);
                    stepContribution.getStepExecution().getJobExecution().getExecutionContext().put("from", "step2");
                    return RepeatStatus.FINISHED;
                }).build();
    }

    private Step step3() {
        return stepBuilderFactory.get("step3")
                .tasklet((stepContribution, chunkContext) -> {
                    // 获取参数
                    Map<String, Object> paramMap = chunkContext.getStepContext().getJobParameters();
                    log.info("param={}", paramMap);
                    System.out.println("执行步骤三操作。。。");
                    // 获取step内设置的参数
                    Map<String, Object> stepParamMap = chunkContext.getStepContext().getJobExecutionContext();
                    String from = (String) stepParamMap.get("from");
                    String uuid = (String) stepParamMap.get("nonce");
                    log.info("from={},uuid={}", from, uuid);
                    return RepeatStatus.FINISHED;
                }).build();
    }
}
